package pl.streamsoft.currencyApp.services;

import java.util.Collection;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import pl.streamsoft.currencyApp.model.Country;
import pl.streamsoft.currencyApp.model.DataBaseConector;

public class CountryService implements ICountryService {

	private final SessionFactory sessionFactory = DataBaseConector.getInstanceOfSesionFactory();

	private Session getSession() {
		return sessionFactory.openSession();
	}
	
	public CountryService reset(){
		DataBaseConector.reset();
		return this;
	}

	@Override
	public Country getCountryByName(String name) {

		Session session = getSession();

		try {

			session.beginTransaction();

			Query query = session.getNamedQuery("Country.getCountriesByName");
			query.setMaxResults(1);
			query.setString("countryName", name);

			Country country = (Country) query.uniqueResult();
			if (country != null) {
				return country;
			}

			country = new Country(name);
			session.save(country);

			return country;

		} finally {
			session.getTransaction().commit();
			session.close();
		}
	}

	@Override
	public Collection<Country> getCountries() {

		Session session = getSession();

		try {

			session.beginTransaction();
			Query query = session.createQuery("from Country");

			return query.list();

		} finally {
			session.getTransaction().commit();
			session.close();
		}
	}

	@Override
	public Collection<Country> getCountries(int minNumberOfCurrency) {

		Session session = getSession();

		try {

			session.beginTransaction();
			Query query = session
					.getNamedQuery("Country.getCountriesByMinValueOfCurrencies");
			query.setInteger("minValueOfCurrencies", minNumberOfCurrency);

			return query.list();

		} finally {
			session.getTransaction().commit();
			session.close();
		}
	}

	@Override
	public void remove(Country country) {

		Session session = getSession();

		try {

			session.beginTransaction();
			session.delete(country);

		} finally {
			session.getTransaction().commit();
			session.close();
		}
	}

	@Override
	public void update(Country country) {

		Session session = getSession();

		try {

			session.beginTransaction();
			session.update(country);

		} finally {
			session.getTransaction().commit();
			session.close();
		}
	}

	@Override
	public void addCountry(Country country) {

		Session session = getSession();

		try {

			session.beginTransaction();
			session.save(country);

		} finally {
			session.getTransaction().commit();
			session.close();
		}
	}

	@Override
	public boolean checkByNameIfExists(String name) {

		Session session = getSession();

		try {

			session.beginTransaction();
			Query query = session.getNamedQuery("Country.getCountriesByName");
			query.setMaxResults(1);
			query.setString("countryName", name);

			return query.uniqueResult() != null;

		} finally {
			session.getTransaction().commit();
			session.close();
		}

	}
}
